<template>
  <!-- 出铁实际重量填报表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="800px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-row>
        <el-col :span="12">
          <el-form-item label="班次" prop="shiftName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.shiftName"
              placeholder="请输入班次"
              clearable
            />

            <span v-else>{{ formData.shiftName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="日期" prop="ironDate">
            <el-date-picker
              v-if="!props.isReadonly"
              v-model="formData.ironDate"
              type="date"
              placeholder="请选择日期"
              style="width: 100%"
            />
            <span v-else>{{ formData.ironDate }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="时间" prop="ironDateTime">
            <el-date-picker
              v-if="!props.isReadonly"
              v-model="formData.ironDateTime"
              type="datetime"
              placeholder="请选择时间"
              style="width: 100%"
            />
            <span v-else>{{ formData.ironDateTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品名称" prop="materialName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.materialName"
              placeholder="请输入产品名称"
              clearable
            />

            <span v-else>{{ formData.materialName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品包次" prop="materialPack">
            <el-inputNumber
              min="0"
              style="width: 250px"
              v-if="!props.isReadonly"
              v-model="formData.materialPack"
              placeholder="请输入产品包次"
              clearable
            />

            <span v-else>{{ formData.materialPack }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出铁时间" prop="ironTime">
            <el-inputNumber
              min="0"
              style="width: 250px"
              v-if="!props.isReadonly"
              v-model="formData.ironTime"
              placeholder="请输入出铁时间"
              clearable
            />

            <span v-else>{{ formData.ironTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出铁目标重量" prop="targetWeight">
            <el-inputNumber
              min="0"
              style="width: 250px"
              v-if="!props.isReadonly"
              v-model="formData.targetWeight"
              placeholder="请输入出铁目标重量"
              clearable
            />

            <span v-else>{{ formData.targetWeight }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出铁实际重量" prop="actualWeight">
            <el-inputNumber
              min="0"
              style="width: 250px"
              v-if="!props.isReadonly"
              v-model="formData.actualWeight"
              placeholder="请输入出铁实际重量"
              clearable
            />

            <span v-else>{{ formData.actualWeight }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出铁目标重量上限" prop="actualPackage">
            <el-inputNumber
              min="0"
              style="width: 250px"
              v-if="!props.isReadonly"
              v-model="formData.actualPackage"
              placeholder="请输入出铁目标重量上限"
              clearable
            />

            <span v-else>{{ formData.actualPackage }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出铁时间目标" prop="ironTarget">
            <el-inputNumber
              min="0"
              style="width: 250px"
              v-if="!props.isReadonly"
              v-model="formData.ironTarget"
              placeholder="请输入出铁时间目标"
              clearable
            />

            <span v-else>{{ formData.ironTarget }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="包数" prop="packNum">
            <el-inputNumber
              min="0"
              style="width: 250px"
              v-if="!props.isReadonly"
              v-model="formData.packNum"
              placeholder="请输入包数"
              clearable
            />

            <span v-else>{{ formData.packNum }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出铁时间异常" prop="ironAbnormalTime">
            <el-inputNumber
              min="0"
              style="width: 250px"
              max="1"
              v-if="!props.isReadonly"
              v-model="formData.ironAbnormalTime"
              placeholder="请输入出铁时间异常"
              clearable
            />

            <span v-else>{{ formData.ironAbnormalTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出铁重量下限-2" prop="targetWeightMin">
            <el-inputNumber
              min="0"
              style="width: 250px"
              v-if="!props.isReadonly"
              v-model="formData.targetWeightMin"
              placeholder="请输入出铁重量下限-2"
              clearable
            />

            <span v-else>{{ formData.targetWeightMin }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出铁重量上限+2" prop="targetWeightMax">
            <el-inputNumber
              min="0"
              style="width: 250px"
              v-if="!props.isReadonly"
              v-model="formData.targetWeightMax"
              placeholder="请输入出铁重量上限+2"
              clearable
            />

            <span v-else>{{ formData.targetWeightMax }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="炉号" prop="stoveNum">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.stoveNum"
              placeholder="请输入炉号"
              clearable
            />

            <span v-else>{{ formData.stoveNum }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出铁重量异常" prop="ironAbnormalWeight">
            <el-inputNumber
              min="0"
              style="width: 250px"
              max="1"
              v-if="!props.isReadonly"
              v-model="formData.ironAbnormalWeight"
              placeholder="请输入出铁重量异常"
              clearable
            />

            <span v-else>{{ formData.ironAbnormalWeight }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="加料后到出铁50KG时间" prop="ingredientsTime">
            <el-inputNumber
              min="0"
              style="width: 250px"
              v-if="!props.isReadonly"
              v-model="formData.ingredientsTime"
              placeholder="请输入加料后到出铁50KG时间"
              clearable
            />

            <span v-else>{{ formData.ingredientsTime }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESMESIRONWEIGHTADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  shiftName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  ironDate: [],
  ironDateTime: [],
  materialName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialPack: [],
  ironTime: [],
  targetWeight: [],
  actualWeight: [],
  actualPackage: [],
  ironTarget: [],
  packNum: [],
  ironAbnormalTime: [],
  targetWeightMin: [],
  targetWeightMax: [],
  stoveNum: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  ironAbnormalWeight: [],
  ingredientsTime: [],
  version: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  createTime: [],
  createdUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  createdUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  lastUpdateTime: [],
  lastUpdatedUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  enabled: [],
  deleted: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  shiftName: "",
  ironDate: "",
  ironDateTime: "",
  materialName: "",
  materialPack: "",
  ironTime: "",
  targetWeight: "",
  actualWeight: "",
  actualPackage: "",
  ironTarget: "",
  packNum: "",
  ironAbnormalTime: "",
  targetWeightMin: "",
  targetWeightMax: "",
  stoveNum: "",
  ironAbnormalWeight: "",
  ingredientsTime: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        shiftName,
        ironDate,
        ironDateTime,
        materialName,
        materialPack,
        ironTime,
        targetWeight,
        actualWeight,
        actualPackage,
        ironTarget,
        packNum,
        ironAbnormalTime,
        targetWeightMin,
        targetWeightMax,
        stoveNum,
        ironAbnormalWeight,
        ingredientsTime,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        shiftName,
        ironDate,
        ironDateTime,
        materialName,
        materialPack,
        ironTime,
        targetWeight,
        actualWeight,
        actualPackage,
        ironTarget,
        packNum,
        ironAbnormalTime,
        targetWeightMin,
        targetWeightMax,
        stoveNum,
        ironAbnormalWeight,
        ingredientsTime,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.shiftName = "";
  formData.ironDate = "";
  formData.ironDateTime = "";
  formData.materialName = "";
  formData.materialPack = "";
  formData.ironTime = "";
  formData.targetWeight = "";
  formData.actualWeight = "";
  formData.actualPackage = "";
  formData.ironTarget = "";
  formData.packNum = "";
  formData.ironAbnormalTime = "";
  formData.targetWeightMin = "";
  formData.targetWeightMax = "";
  formData.stoveNum = "";
  formData.ironAbnormalWeight = "";
  formData.ingredientsTime = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 出铁实际重量填报保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/mesironweight/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
